package com.example.zg6.bottomnavgation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.zg6.AppConfig;
import com.example.zg6.R;
import com.example.zg6.entity.MainTabsEntity;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import java.util.List;

public class AppBottomView extends BottomNavigationView {
    private int[] icons = {R.drawable.icon_tab_home, R.drawable.icon_tab_sofa, R.drawable.icon_tab_publish,
            R.drawable.icon_tab_find, R.drawable.icon_tab_mine};

    public AppBottomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        createItem();
    }


    //创建item方法
    @SuppressLint("RestrictedApi")
    private void createItem() {
        MainTabsEntity mainTabsEntity = AppConfig.getInstance().mainTabsEntity;
        //获取选中未选择颜色
        //解析rgb颜色为对应int值
        int[] colors = {
                Color.parseColor(mainTabsEntity.activeColor),
                Color.parseColor(mainTabsEntity.inActiveColor)};

        int[][] state = new int[2][];//配置对应选中状态
        state[0] = new int[]{android.R.attr.state_selected};//选中状态
        state[1] = new int[]{-android.R.attr.state_selected};//未选中状态
        //设置文字和图片选中未选中颜色
        ColorStateList stateList = new ColorStateList(state, colors);
        setItemTextColor(stateList);
        setItemIconTintList(stateList);
        List<MainTabsEntity.Tabs> tabs = mainTabsEntity.tabs;
        //添加当前的menuitem
        for (MainTabsEntity.Tabs tab : tabs) {
            if (!tab.enable)
                continue;
            int id = readDestinationId(tab.pageUrl);
            if (id==-1)
                continue;

            MenuItem item = getMenu().add(0, id, tab.index, tab.title);
            item.setIcon(icons[tab.index]);
        }

        //修改掉默认上跳效果以及隐藏title效果
        int index = 0;
        for (MainTabsEntity.Tabs tab : tabs) {
            int size = ConvertUtils.dp2px(tab.size);
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(index);
            //设置图标大小
            itemView.setIconSize(size);
            itemView.setShifting(false);
            menuView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
            int tintColor;
            if (TextUtils.isEmpty(tab.title))
                tintColor = Color.parseColor("#ff678f");
            else
                tintColor = Color.parseColor("#666666");
            itemView.setIconTintList(ColorStateList.valueOf(tintColor));

            index++;
        }
        if (mainTabsEntity.selectTab>=0){
            if (mainTabsEntity.tabs.get(mainTabsEntity.selectTab).enable){
                String url = mainTabsEntity.tabs.get(mainTabsEntity.selectTab).pageUrl;
                int id = readDestinationId(url);
                setSelectedItemId(id);
            }
        }

    }


    //根据pageUrl 获取fragment id放法
    private int readDestinationId(String pageUrl) {
        if (TextUtils.isEmpty(pageUrl))
            return -1;
        int id = AppConfig.getInstance().destinationMap.get(pageUrl).id;
        return id;
    }


}
